/**
 * Created by 11450 on 2017/4/23.
 */
module.controller('GetWebhookURLController',function ($scope, $rootScope, $cookies,$state,$stateParams,params,$mdDialog,updateWebhook) {
    var data = params.data;
    var username = $cookies.get('username');
    $scope.hideBtn = params.btn;

    var urls = window.location.host;
    var webhookURL =  "http://" + urls +"/opBuildInfo/webhook/" + data._id;
    var path = data.gitPath;
    if(isContains(path,'bitbucket.org') === true){
        $scope.urlType = 'bitbucket';
        $scope.webhookURL = webhookURL;
    }else if(isContains(path,'github.com') === true){
        $scope.urlType = 'github';
        $scope.webhookURL = webhookURL;
    }

    $scope.copyUrl = function () {
        //复制url
        var whURL = angular.element(document.getElementById('whURL'));
        whURL.select();
        document.execCommand("Copy");

        var coptbtn = angular.element(document.getElementById('copybtn'));
        coptbtn.title = '已复制';
    };

    $scope.cancel = function () {
        //点击确定，不做操作
        $mdDialog.cancel();
    };

    $scope.closeCI = function () {
      //关闭持续集成
        updateWebhook(username,data.id).then(function (res) {
            if(res.data.ok === 1 && res.data.nModified === 1 &&res.data.n === 1){
                alertify.success("持续集成关闭成功");
            }else{
                alertify.error("请求失败");
            }
        },function (err) {
            alertify.error(err);
        });

        $mdDialog.cancel();
    };

    // $scope.closeDialog = function () {
    //     //关闭弹框
    //     $mdDialog.cancel();
    // };

    function isContains(str,substr) {
        return str.indexOf(substr) >= 0;
    }

});